 ..
WIN32 API 
 , 2009.




      Microsoft Windows   C++   Win32 API. 

   Windows  - .     ,    ,   .          .

      Windows     (handle).      .           .

   (API  Application Pro- gramming Interface)    32-  (Win32 API),      (),    Microsoft Windows.     .    -  windows.h,        .

 Win32      -       (  ).    ,  .

 -     (    ).          ,   , ,  ,  .

  ,   ,   ,      .

  Windows             .

   :
  (overlapped window);
  (popup window);
  (child window);
  (layered window)   ,     ,  .

    CreateWindowEx()   WS_OVERLAPPEDWINDOW.     ,  ,    ,      ,    .           .

    CreateWindowEx()   WS_POPUP     ,       ,       .       ,    ,     -    WS_POPUPWINDOW | WS_CAPTION.

    CreateWindowEx()   WS_CHILD             .     ,  ,    ,    ,     .

         , ..          .     ,      . 

 :
 SDI (Single Document Interface)     ; 
 MDI (Multiple Document Interface)     ; 
   (Based Dialog) -       ,    ,  ,   . 

Windows        ANSI ,     UNICODE.   UNICODE     ,    65536 .  

      ANSI    UNICODE     :

1.     tchar.h;
2.        TCHAR, LPTSTR  LPCTSTR;
3.         _TEXT (  _T); 
4.  ,  sizeof(szBuffer)     ,  sizeof(szBuffer)/sizeof(TCHAR)     . 

  TCHAR, LPTSTR  LPCTSTR  -  :

typedef unsigned short wchar_t;
typedef wchar_t WCHAR;
#define UNICODE
   typedef WCHAR TCHAR; //UNICODE 
#else
   typedef char TCHAR; //ANSI 
#endif
typedef WCHAR* LPWSTR;
typedef CHAR* LPSTR;
#define UNICODE
   typedef LPWSTR LPTSTR; //UNICODE 
#else
   typedef LPSTR LPTSTR; //ANSI 
#endif
typedef CONST WCHAR* LPCWSTR;
#define UNICODE
   typedef LPCWSTR LPCTSTR; //UNICODE 
#else
   typedef LPCSTR LPCTSTR; //ANSI 
#endif

  ,      ,  ANSI  UNICODE , ,   lstrcat     :

LPTSTR lstrcat (
LPTSTR lpString1,
LPTSTR lpString2
);

 Win32   ANSI  UNICODE , ,  DispatchMessage():

# ifdef UNICODE
#define DispatchMessage DispatchMessageW
#else
#define DispatchMessage DispatchMessageA
#endif

,     ,        ANSI    UNICODE ,        Microsoft Windows XP Professional   Microsoft Visual Studio 2005. 



1.    WINDOWS

Windows      , ,  ,      :  WinMain()    WndPoc().



1.1.   WinMain()

 WinMain()     .  -     winbase.h  :

int WINAPI WinMain(
HINSTANCE hInstance,     //  
HINSTANCE hPrevInstance, //   
LPSTR lpszCmdLine,       //    
int nCmdShow             //,    
);

 WINAPI    , ..   Win32      .    ,  , ..      ,      .

  WinMain(), Windows     ().

  hInstance  HINSTANCE      .        Windows     .   Win32 API        .    (hInstance)    , ..   ,    hInstance   : 

    HINSTANCE hInst;
       GetClassLong(), :
hInst=(HINSTANCE)GetClassLong(hWnd,GCL_HMODULE);
       GetModuleHandle()  : 
hInst= GetModuleHandle(NULL);

  - hPrevInstance -       .

  - lpszCmdLine -     ,      ,      .

  - nCmdShow -   .

,        WinMain(),     ,     . 

   Windows   WinMain()      :

1)       ;          ,       (Windows        ); 
2)           (          );
3)     ,   .



1.2.     

  (window class),   ,     WNDCLASSEX,    .           ,     , , ,       .         ,     .

         WNDCLASSEX    RegisterClassEx(),      .     winuser.h: 

typedef struct tagWNDCLASSEX
{
UINT cbSize;           //    
UINT style;            //   
WNDPROC lpfnWndProc;   //   
int cbClsExtra;        //       
int cbWndExtra;        //         
HINSTANCE hInstance;   //   
HICON hIcon;           //   
HCURSOR hCursor;       //  
HBRUSH hbrBackground;  //      
LPCTSTR lpszMenuName;  //       
LPCTSTR lpszClassName; //       
HICON hIconSm;         //  
} WNDCLASSEX;

    WNDCLASSEX   32-  .

         .     WNDCLASSEX       ,     .     Windows,        .       ,  OPENFILENAME,         ,  LOGFONT,     . 

             memset() ZeroMemory() ,    : 

VOID ZeroMemory(
PVOID pBuffer,    //   ,    
SIZE_T length     //    
);
void *memset( void *dest, int c, size_t count );

: memset(&wc, 0, sizeof(wc));

       -   WNDCLASSEX,     .

   cbSize  WNDCLASSEX       .      -     .

  style  WNDCLASSEX     (32 ).           ( ).         .    Windows      CS_. :

 CS_DBLCLKS     (0x00000008),           ; 
 CS_VREDRAW     (0x00000001),            ; 
 CS_HREDRAW     (0x00000002),            ; 
 CS_NOCLOSE     (0x00000200),     .

    Ȼ  C++      (style). 

         WNDCLASSEX:

 lpfnWndProc     ( );
 hInstance     ( );
 lpszClassName        (11- ).

     WNDCLASSEX ( lpfnWndProc)      ,         .
 
   - hIcon -   .      ,      .

    : 
 16x16    ,  16-;
 32x32    ,  16-;
 48x48     256 .


     

HICON LoadIcon(
HINSTANCE hInst,    //   
LPCTSTR lpszIcon    //  ,      
);

        (.exe)    (hInst), :

wc.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_APPICON));

  - lpszIcon,   ,      .    ,     ,        .            MAKEINTRESOURCE,     winuser.h  :

#define MAKEINTRESOURCE(i) \ (LPSTR) ((DWORD)((WORD)(i)))

    ,    16     .
 
     hInst  LoadIcon()    NULL.        ,      IDI_ (ID for icon), :
 
wc.hIcon = LoadIcon(hInst, IDI_APLICATION);

   .  MSDN.       LoadIcon()   ,      NULL.
 
 ,   LoadIcon()      , ..    -    16x16 .     ,       LoadImage(),    :
 
HANDLE LoadImage(
HINSTANCE hInst,    //   
LPCTSTR lpszName,   //  ,    ,  
UINT uType,         //   
int cxDesired,      //     
int cyDesired,      //     
UINT fuLoad         //   
);

  - lpszName    .     hInst  NULL,    fuLoad   LR_LOADFROMFILE,   lpszName   ,      .

  - uType -        :

IMAGE_BITMAP;
IMAGE_CURSOR;
IMAGE_ICON.

    - cxDesired, cyDesired -       .     ,        .

  - fuLoad -   ,        :

 LR_DEFAULTCOLOR    ,       ( ); 
 LR_DEFAULTSIZE     cxDesired  cyDesired  ,        ; 
 LR_LOADTRANSPARENT      (     COLOR_WINDOW);
 LR_LOADFROMFILE      .

    16x16     :

wc.hIcon = (HICON)LoadImage( Inst,
                  MAKEINTRESOURCE(IDI_APPICON_SM),
                  IMAGE_ICON,16, 16,0);

    :

HBITMAP hBitmap (HBITMAP)LoadImage(hInst,
                         FILE_NAME, IMAGE_BITMAP,
                         100, 100, LR_LOADFROMFILE);

  hCursor  WNDCLASSEX    .     Windows -     32x32 .      LoadCursor(),    : 

HCURSOR LoadCursor(
HINSTANCE hInst,     //   
LPCTSTR lpszCursor   //  ,    
);

   ,    lpszCursor   ,    hInst.

     ()       NULL,     ,      IDC_. 

,          IDC_ARROW. ,  , -   ,    : wc.hCursor = LoadCursor(NULL, IDC_ARROW);

     :

IDC_ARROW      ;
IDC_CROSS     ;
IDC_SIZEALL    ;
IDC_SIZENS      ();
IDC_IBEAM      ;
IDC_WAIT       .

            SetCursor(),    :

HCURSOR SetCursor(HCURSOR hCursor);

 SetCursor   ,      ,       .
 
      WM_MOUSMOVE.         .
 
,              WM_CREATE, WM_SIZE  WM_MOUSMOVE   ( hCursor1, hCursor2, wClient, hClient, xPos  yPos         static):

Case WM_CREATE:
//
hCursor1 = LoadCursor(NULL,IDC_SIZEALL);
hCursor2 = LoadCursor(NULL, IDC_WAIT);
//
case WM_SIZE:
//    
wClient = LOWORD(lParam);
hClient = HIWORD(lParam);
break;
case WM_MOUSMOVE:
//  
xPos = LOWORD(lParam);
yPos = HIWORD(lParam);
if(yPos < 32)
//  
SetCursor(hCursor1);
if(yPos > hClient  32)
//  
SetCursor(hCursor2);
break;
//

     ,    hbrBackground  WNDCLASSEX. 

 (brush)    ,       .

   ,   , ,  , ,   ,  Windows    HGDIOBJ. 

 GetStockObject()         :

HGDIOBJ GetStockObject(int nObjectTyp);

    GetStockObject()     (HPEN,HBRUSH, HBITMAP, HPALETTE, HFONT, HRGN).

,         :
ws.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

  windowsx.h   GetStockBrush, GetStockPen  GetStockFont,    , :
#define GetStokBrush(i)(HBRUSH)GetStockObject(i))

      GetStockObject()  :
ws.hbrBackground = GetStockBrush(WHITE_BRUSH);

 Windows    () :

WHITE_BRUSH      ;
DKGRAY_BRUSH    - ;
LTGRAY_BRUSH    - ;
GRAY_BRUSH       ;
BLACK_BRUSH      ;
NULL_BRUSH      .

       CreateSolidBrush(),    :
HBRUSH CreateSolidBrush(COLORREF crColor);

 crColor   RGB   .       ,   ,     .         (4 )   RGB, :
 
ws.hbrBackground= CreateSolidBrush(RGB(0,255,255));

      Regis- terClassEx(),    : 
ATOM RegisterClassEx(CONST WNDCLASSEX* lpwc);

  lpw      WNDCLASSEX.

 RegisterClassEx()    ATOM (WORD  16 ),      .

          WM_CREATE  SetClassLong()  SetWindowLong(),    :

DWORD SetClassLong(
      HWND hWnd,       // 
      int nIndex,      // ,    
      LONG dwNewLong   // 
);
LONG SetWindowLong(
     HWND hWnd,        // 
     int nIndex,       // ,  //  
     LONG dwNewLong    // 
);

 nIndex  SetClassLong()    :

 GCL_HBRBACKGROUND         ; 
 GCL_HCURSOR             ;
 GCL_HICON               ;
 GCL_HICONSM              ;
 GCL_MENUNAME             ;
 GCL_STYLE                ;
 GCL_WNDPROC              ,   .

,          : 

Case WM_CREATE:
     SeClassLong(hWnd,GCL_HBRBACKGROUND,
                (LONG)CreateSolidBrush(RGB(0,250,250)));
     return TRUE;
//



1.3.    

   ,       ,   Windows CreateWindowEx(),    :

HWND CreateWindowEx(
     DWORD dwExStyle,     //  , .    dwStyle
     LPCTSTR lpClassName, //     
                          //      
     LPCTSTR lpWindowName //      
     DWORD dwStyle,       // 
     int x                //      
     int y,               //      
     int nWidth,          //   
     int nHeight,         //    
     HWND hWndParent,     //  
     HMENU hMenu,         //       
     HINSTANCE hInstance, //   
     LPVOID lpParam       //    
);

  - dwExStyle -    ,       ,    .   :
 
 WS_EX_MDICHILD            ; 
 WS_EX_TOOLWINDOW         ,      ; 
 WS_EX_ACCEPTFILES      ,    .

  dwExStyle    ,     dwStyle.     .  MSDN.

  - lpClassName    ,     RegisterClassEx() ,      .

  CreateWindowEx() ,      , , ,     .       .   ,    :
 
    BUTTON  , , ,    (     BS_);
    COMBOBOX              ( LISTBOX  EDIT) (     CBS_);
    EDIT   ,       (    ES_);
    LISTBOX     ,          ( LBS_OWNERDRAWFIXED  LBS_OWNERDRAW  -   ) (    LBS_);
    MDICLIENT     ;
    RICHEDIT       EDIT -         (     ES_); 
    RICHEDIT_CLASS    RICHEDIT (     ES_);
    SCROLBAR      () (     SBS_);
    STATIC      - -      ,         (     SS_).

  - lpWindowName  CreateWindowEx() -      .       , :

             ; 
    BUTTON      .

  dwStyle   .       ,     ,   ,        ..

   ( |)     .      WS_OVERLAPPEDWINDOW(0x00CF0000).     ,  ,    ,      ,    .

  x    y        , ..      .                   .   x  ,     x  CW_USEDEFAULT    .     y .

    nWidth  nHeight       .   nWidth  nHeight   CW_USEDEFAULT,        .

  hWndParent     ;   ,    ,   HWND_DESKTOP  NULL.

  hMenu  CreateWindowEx()        .    hMenu    :
   ,   lpszMenu    WNDCLASSEX ( ).     hMenu    NULL;

       .     hMenu   ,       (      WM_COMMAND,     - ).

  hInstance        (  hInstance   WinMain).

  lpParam  CreateWindowEx()     ,          WM_CREATE,   NULL.

  WM_CREATE  WM_NCCREATE  lParam     CREATESTRUCT,    -    CreateWindowEx().

  NULL ((void*)0)    ,         ,  .

 CreateWindowEx()       , ..   hWnd.   hWnd      ShowWindow(),       .  ShowWindow()   :

BOOL ShowWindow(
     HWND hWnd,    //  
     int nCmdShow  //   
);

 nCmdShow ,      .          ,      nCmdShow   WinMain.
 ShowWindow()      WM_SIZE  WM_MOVE.    WM_SIZE     WM_PAINT.